//
//  WMUpdateView.m
//  WMUpdateView
//
//  Created by mac on 16/2/21.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "WMUpdateView.h"

@interface WMUpdateView ();

@property(nonatomic,copy)WMUpdateViewBlock confirmBlock;
@property(nonatomic,copy)WMUpdateViewBlock cancleBlock;


@property(nonatomic,strong) UIVisualEffectView *coverView;

@end




#define kMainScreenW [UIScreen mainScreen].bounds.size.width
#define kMainScreenH [UIScreen mainScreen].bounds.size.height

@implementation WMUpdateView

- (UIView *)coverView{
    
    if (_coverView == nil) {
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        _coverView = [[UIVisualEffectView alloc] initWithEffect:blur];
        _coverView.frame = [UIScreen mainScreen].bounds;
    }
    return _coverView;
}


- (instancetype)initWithTitle:(NSString *)title
                     imageUrl:(NSString *)imageUrl
                  contentText:(NSString *)contentText
                 confirmTitel:(NSString *)confirmTitle
                         type:(WMUpdateViewType)updateViewType
                 confirmBlock:(WMUpdateViewBlock)confirmBlock
                  cancleBlock:(WMUpdateViewBlock)cancleBlock{
    
    self.confirmBlock = confirmBlock;
    self.cancleBlock = cancleBlock;

    if (self = [super init]) {
        
    
#define kUpdateViewW 240
#define kUpdateViewH 320
    
        // self
        self.layer.cornerRadius = 5;
        self.backgroundColor = [UIColor whiteColor];
        
        
        // image
        
        
        // title
        
        
        // button
        UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat btnW = 180;
        CGFloat btnH = 40;
        CGFloat btnX = (kUpdateViewW - btnW) * 0.5;
        CGFloat btnY = (kUpdateViewH - btnH - 15);
        confirmBtn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        
        confirmBtn.layer.cornerRadius = 3;
        confirmBtn.layer.masksToBounds = YES;
        
        [confirmBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:227.0/255.0 green:100.0/255.0 blue:83.0/255.0 alpha:1]] forState:UIControlStateNormal];
        
        [confirmBtn setTitle:confirmTitle forState:UIControlStateNormal];
        
        [confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
        [self addSubview:confirmBtn];
        
        // xbutton
        
    }

    return self;
}

- (void)show{

    // 寻找显示的正在显示的控制器
    UIViewController *topVc = [self seekTopViewController];
    // 设置self的位置
    CGFloat updateX = (kMainScreenW - kUpdateViewW) * 0.5;
    CGFloat updateY = (kMainScreenH - kUpdateViewH) * 0.5;
    self.frame = CGRectMake(updateX, updateY, kUpdateViewW, kUpdateViewH);
    
    [topVc.view addSubview:self];
}

- (UIViewController *)seekTopViewController{

    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVc = rootVc;
    // 循环
    while (topVc.presentedViewController) {
        topVc = rootVc.presentedViewController;
    }
    return topVc;
}

- (void)willMoveToSuperview:(UIView *)newSuperview{
    
    if (newSuperview == nil) {
        return;
    }
    // 在添加到view上之前 添加cover
    UIViewController *topVc = [self seekTopViewController];
    [topVc.view addSubview:self.coverView];
    
    // 动画
    self.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        self.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
    }];

    [super willMoveToSuperview:newSuperview];
    
}


- (void)confirmBtnClick:(UIButton *)button{
    
    [self dismiss];
    if (self.confirmBlock) {
        self.confirmBlock();
    }

}

- (void)dismiss{
    [self removeFromSuperview];
}

- (void)removeFromSuperview{
    
    [self.coverView removeFromSuperview];
    self.coverView = nil;
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        // 注意：这里写成(0,0)会失去动画效果
        self.transform = CGAffineTransformMakeScale(0.3, 0.3);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [super removeFromSuperview];

    }];

}


@end

@implementation UIImage(colorful)

+ (UIImage *)imageWithColor:(UIColor *)color{

    UIImage *image = nil;
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
// Paints the area contained within the provided rectangle, using the fill color in the current graphics state
    CGContextFillRect(context, rect);
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end



